<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
$process_unit = sxREQ("pu");
if($process_unit == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("manca cod. 'pu'"));
	serverRedir($url);
}
if(sxREQ("reqtype") == "delete"){
	$errMsg .= puDel();
}
$sSQL = "SELECT p.*, m.description AS mdes, sm.functional_area, " .
		"	fa.description AS fades, sm.description AS smdes " .
		" FROM process_unit p" .
		" LEFT JOIN module m ON m.module = p.module " .
		" LEFT JOIN sub_module sm ON m.module = p.module " .
		"    AND sm.sub_module = p.sub_module " .
		" LEFT JOIN functional_area fa ON fa.module = p.module " .
		"    AND fa.functional_area = sm.functional_area " .
		" WHERE process_unit = '".$process_unit."' ";
$objSDAO->query($sSQL, "mod");
if(!$row = $objSDAO->fetch("mod")) {
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("process-unit '%s' missing",$process_unit));
	serverRedir($url);
}
$module = $row["module"];
$sub_module = $row["sub_module"];
$type = $row["type"];
$description = $row["description"];
$note = $row["note"];			
//documents
$sSQL = "SELECT * FROM process_unit_doc WHERE process_unit='$process_unit'";
$objSDAO->query($sSQL, "doc");
//frames
$sSQL = "SELECT * FROM frame WHERE process_unit='$process_unit'";
$objSDAO->query($sSQL, "frame");
//links
$sSQL = "SELECT pu.* FROM pu_link pu WHERE pu.process_unit = '$process_unit' ORDER BY pu.group , pu.sequence";
$objSDAO->query($sSQL, "link");
$sSQL = "SELECT pa.*, e.description AS e_des FROM pu_alias pa "
	." LEFT JOIN entity e ON e.entity = pa.entity "
	." WHERE process_unit='$process_unit' " .
		" ORDER BY alias";
$objSDAO->query($sSQL, "alias");
//entry points
$sSQL = "SELECT ep.* FROM entry_point ep " .
		" WHERE ep.process_unit='".$process_unit."' ORDER BY ep.entry_point";
$objSDAO->query($sSQL, "entp");

//output HTML
$aOpz = array("kMenu"=>'pro.pru');
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function confDel(){
	if (confirm('Comfirm?')) {
		document.location.href = "puView.php?pu=<?=sxRHE('pu')."&reqtype=delete&xsn=".XSN?>";
	}
}
</script>
<?php
echo getSubHeader($module,$sub_module);

$aCommand = array(	
	"layo"=>array("pag"=>"../layo/layoutList.php?mod=".substr($process_unit,0,4),
						"title"=>_t("list of layout"),
						"text"=>_t("list of layout"),
						"perm"=>"viewmod","align"=>"L"),
	"del"=>array("pag"=>"javascript:confDel();",
						"title"=>_t("delete processo"),
						"text"=>_t("delete"),
						"perm"=>"viewmod"),
	"mod"=>array("pag"=>"puMod.php?pu=$process_unit",
						"title"=>_t("change processo"),
						"text"=>_t("change"),
						"perm"=>"viewmod"),
	"back"=>array("pag"=>$objUser->getHistory(-1, false),
						"title"=>$objUser->getHistoryText(-1),
						"text"=>$objUser->getHistoryText(-1)
						)
					);
echo(commandsHTML($aCommand));	
echo("<table width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Precess-Unit</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='15%'>Name</td>" .
			"<td class=\"text01\" width='85%'>$process_unit ($type)</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\">" .sxHE($description)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Type</td>" .
			"<td class=\"text01\">$type</td>" .
		"</tr>\n");
if (!empty($note)) {
	echo("<tr>" .
				"<td class=\"labl01\">"._t("Note")."</td>" .
				"<td class=\"text01\"><pre class=\"text01\">".sxWW(sxHE($note))."</pre></td>" .
			"</tr>\n");
}
$row = $objSDAO->fetch("doc");
if ($row) {
	echo("<tr>" .
				"<td class=\"titl01\">"._t("Documenti")."</td>" .
				"<td class=\"tabh01\">\n");
	$numDoc = 0;
	while($row){
		$numDoc++;
		echo("<a class=\"text01\" target=\"blank\" href=\"getDoc.php?pu=$process_unit&fn=".$row["file_name"]."&xsn=".XSN."\">".sxHE($row["name"])."</a></td>" .		
			"<br>\n");
		$row = $objSDAO->fetch("doc");
	}
	echo "</td></tr>\n";
}
	echo "</table>\n";
//link 
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aCmd = array("pag"=>"puLinkIns.php?pu=$process_unit",
						"title"=>_t("add new link"),
						"text"=>_t("new link"),
						"perm"=>"viewmod");
echo getTitleLink('Link', $aCmd);
echo("<tr>" .
			"<td class=\"tabh01\" width='5%'>"._t("Group")."</td>" .
			"<td class=\"tabh01\" width='5%'>"._t("Sequence")."</td>" .
			"<td class=\"tabh01\" width='20%'>"._t("Name")."</td>" .
			"<td class=\"tabh01\" width='20%'>"._t("Label")."</td>" .
			"<td class=\"tabh01\" width='50%'>"._t("Linked process-unit")."</td>" .	
		"</tr>\n");
$num = 0;
while($row = $objSDAO->fetch("link")){
	$num++;
	echo("<tr>" .
			"<td class=\"text01\">".$row["group"]."</td>" .
			"<td class=\"text01\">".$row["sequence"]."</td>" .
			"<td class=\"text01\"><a class=\"text01\" href=\"puLinkView.php?pu=$process_unit&lnk=".$row["name"]."&xsn=".XSN."\">".$row["name"]."</a></td>" .		
			"<td class=\"text01\">".sxHE($row["label"])."</td>" .
			"<td class=\"text01\">".
			(($row["pu_linked"]=='- back -')?'- previus -':(($row["pu_linked"]=='- self -')?'- self -':$row["pu_linked"])).
			"</td>" .
		"</tr>\n");
}
if($num == 0){
	echo("<tr>" .
			"<td class=\"text01\" colspan=\"5\">"._t("Nessun link")."</td>" .			
		"</tr>\n");
}
//frames
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aCmd = array("pag"=>"../fram/frameIns.php?pu=$process_unit",
						"title"=>_t("add new frame"),
						"text"=>_t("new frame"),
						"perm"=>"viewmod");
echo getTitleLink('Frame', $aCmd);
echo("<tr>" .
			"<td class=\"tabh01\">"._t("Name")."</td>" .	
			"<td class=\"tabh01\">"._t("Layout")."</td>" .	
			"<td class=\"tabh01\">"._t("Mode")."</td>" .	
			"<td class=\"tabh01\">"._t("Description")."</td>" .	
		"</tr>\n");
$num = 0;
while($row = $objSDAO->fetch("frame")){
	$num++;
	echo("<tr>" .
			"<td class=\"text01\">" .
			"<a class=\"text01\" href=\"../fram/frameView.php?pu=$process_unit&frame=".$row["frame"]."&xsn=".XSN."\">".
				$row["frame"]."</a></td>" .		
			"<td class=\"text01\">" .
			"<a class=\"text01\" href=\"../layo/layoutSetView.php?set=".$row["layout_set"]."&xsn=".XSN."\">".
			$row["layout_set"]."</a></td>" .		
			"<td class=\"text01\">".(($row["display_only"]=='Y')?'display':'update')."</td>" .		
			"<td class=\"text01\">".$row["description"]."</td>" .		
		"</tr>\n");
}
if($num == 0){
	echo("<tr>" .
			"<td class=\"text01\" colspan='4'>"._t("No frame")."</td>" .			
		"</tr>\n");
}
echo("</table>");
$aCmd = array("pag"=>"puAliasIns.php?pu=$process_unit",
						"title"=>_t("add new alias"),
						"text"=>_t("new alias"),
						"perm"=>"viewmod",
						"align"=>"R");

echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
//aliases
echo getTitleLink('List of Aliases', $aCmd);
echo("<tr>" .
		"<td class=\"tabh01\" width=\"1%\">"._t('Alias')."</td>" .
		"<td class=\"tabh01\" width=\"1%\">"._t('R/O')."</td>" .
		"<td class=\"tabh01\" width=\"48%\">"._t('Entity')."</td>" .
		"<td class=\"tabh01\" width=\"50%\">"._t('Description')."</td>" .
	"</tr>\n");	
$numElem=0;
$aAliasXYW['W'] = array('W - simple memory variable','not-exists');
$aAliasXYW['X'] = array('X - 1 dimension array memory variables','not-exists');
$aAliasXYW['Y'] = array('Y - 2 dimensions array memory variables','not-exists');
while($row = $objSDAO->fetch("alias")){
	$numElem ++;
	if (isset($aAliasXYW[$row["alias"]])) {
		$aAliasXYW[$row["alias"]][1] = 'exists';
		continue;
	}
	echo("<tr>" .
			"<td class=\"text01\" valign='top' align='center'><a class=\"text01\" ".
				goToPag("puAliasView.php?pu=".$row["process_unit"]."&alias=".$row["alias"]."&xsn=".XSN).
				" title=\""._t("Visualizza alias")."'".sxHE($row["description"])."'\">&nbsp;".
				$row["alias"]."&nbsp;</a></td>\n" .
			"<td class=\"text01\" valign='top'>".(($row["read_only"]=='Y')?'R/O':'&nbsp;')."</td>" .
			"<td class=\"text01\" valign='top'><a class=\"text01\" ".
				goToPag("../../data/enty/entityView.php?da=".substr($row["entity"],0,4)."&ent=".$row["entity"]."&xsn=".XSN).
				" title=\""._t("View entity")."'\">".
				sxHE($row["entity"])."</a></td>" .
			"<td class=\"text01\" valign='top'>".sxHE(ucfirst($row['e_des']).
				((empty($row['description']))?'':" - ".$row['description']))."</td>" .
		"</tr>\n");
}
echo("<tr>" .
			"<td class=\"text01\" colspan=\"20\">");
//dbg_($aAliasXYW);
foreach($aAliasXYW as $kX => $aX) {
	echo "<a class=\"text01\" ".
				goToPag("puAliasView.php?pu=$process_unit&alias=$kX&xsn=".XSN).
				" title=\"Visualizza alias $kX\">".$aX[0]."</a>" .
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	if ($aX[1]=='not-exists') { //alias not defined
		$sSql = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only)"
			." values('$process_unit','$kX','','".addslashes(SUBSTR($aX[0],4))."','','N')";
		//dbg_($sSql);
		$objSDAO->exec($sSql);
	}
}
echo 		"</td>".
		"</tr>\n";
echo("</table>\n");
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aCmd = array("pag"=>"../entp/entpIns.php?pu=$process_unit",
						"title"=>_t("add new entry-point"),
						"text"=>_t("new entry-point"),
						"perm"=>"viewmod",
						"align"=>"R");
echo getTitleLink('Entry-point', $aCmd);
echo("<tr>" .
		"<td class=\"tabh01\" width=\"2%\" align='center'>"._t('Code')."</td>" .
		"<td class=\"tabh01\" width=\"98%\">"._t("Description")."</td>" .
	"</tr>\n");	
$numElem == 0;
while($row = $objSDAO->fetch("entp")) {
	if ($row["entry_point"]=='') { break; }
	$numElem ++;
	echo("<tr>" .
			"<td class=\"text01\" valign='top' align='center'><a class=\"text01\" ".
				goToPag("../entp/entpView.php?pu=".sxHE($row["process_unit"])."&ep=".sxHE($row["entry_point"])."&xsn=".XSN).
				" title=\""._t("visualizza l'entry-point' ")."'\">".
				str_pad(sxHE($row["entry_point"]),4,'0',STR_PAD_LEFT)."</a></td>\n" .
			"<td class=\"text01\" valign='top'>".sxHE($row["description"])."</td>" .
		"</tr>\n");	
}
if($numElem == 0)
	echo("<tr>" .
			"<td class=\"text01\" colspan=\"2\">"._t("No entry-point defined.")."</td>".
		"</tr>\n");
echo("</table>\n");
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aCmd = array("pag"=>"#",
						"title"=>_t("add library"),
						"text"=>_t("add library"),
						"perm"=>"viewmod",
						"align"=>"R");
echo("</table>\n");
echo(commandsHTML($aCommand));

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function puDel(){
	global $objSDAO;
	$errMsg = "";
	$process_unit = sxREQ('pu');
	if($errMsg != ""){return $errMsg;}
	
	//tabelle dipendenti
	$aTabDip['entry_point'][] = 'process_unit';
	$aTabDip['entry_point_alias'][] = 'process_unit';
	$aTabDip['entry_point_alias_match'][] = 'process_unit';
	$aTabDip['entry_point_attribute'][] = 'process_unit';
	$aTabDip['entry_point_rule'][] = 'process_unit';
	$aTabDip['entry_point_rule_attribute'][] = 'process_unit';
	$aTabDip['entry_point_rule_sub_rule'][] = 'process_unit';
	$aTabDip['entry_point_variable'][] = 'process_unit';
	$aTabDip['frame'][] = 'process_unit';
	$aTabDip['frame_alias_match'][] = 'process_unit';
	$aTabDip['menu_line'][] = 'process_unit';
	$aTabDip['process_unit_doc'][] = 'process_unit';
	$aTabDip['pu_alias'][] = 'process_unit';
	$aTabDip['pu_attribute'][] = 'process_unit';
	$aTabDip['pu_link'][] = 'process_unit';
	$aTabDip['pu_variable'][] = 'process_unit';
	$aTabDip['pu_link'][] = 'pu_linked';
	$aTabDip['process_unit'][] = 'process_unit';
	//delete tables
	foreach($aTabDip as $tabX => $aNomeX) {
		foreach($aNomeX as $nomeX) {
			$sSQL = "DELETE FROM $tabX WHERE $nomeX = '$process_unit'";
			$objSDAO->exec($sSQL);
		}
	}
	//delete docs
	$dir = SERVER_FS_DOC."/mb_module";
	$dir .= "/".substr($process_unit,0,4);
	$dir .= "/".substr($process_unit,5,4);
	$dir .= "/".substr($process_unit,10);		
	if ($dh = opendir($dir)) {	
		while($fileX = readdir($dh)){
			if($fileX[0]=='.') continue;
			@unlink($dir."/".$fileX);
		}
		closedir($dh);
		@rmdir($dir);
	}
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return $errMsg;
}
